package com.iraeta.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.entity.main.Tblog;
import com.iraeta.entity.main.Tbwork;
import com.iraeta.entity.main.resulttype.TblogResultType;
import com.iraeta.service.work.TbWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController(value = "TbWorkController")
@RequestMapping("tbwork")
public class TbWorkController extends BaseController{
    @Autowired
    TbWorkService tbWorkService;

    /*分页查看所有任务*/
    @RequestMapping("pagetbwork")
    public String pageTbWork(@RequestBody JSONObject jo){
        this.pageSet(jo);
        List<Tbwork> tbworkList = tbWorkService.listTbwork();
        PageInfo<Tbwork> tbworkPageInfo = this.getPageInfo(tbworkList);
        return this.success(tbworkPageInfo);
    }

    /*分页查看所有日志*/
    @RequestMapping("pagetblog")
    public String pageTbLog(@RequestBody JSONObject paramJb){
        Date searchDate = paramJb.getDate("beginDate");
        Date endDate =  paramJb.getDate("endDate");
        this.pageSet(paramJb);
        List<TblogResultType> tblogList = tbWorkService.listTblog(DateUtil.formatDate(searchDate), DateUtil.formatDate(endDate));
        PageInfo<TblogResultType> tblogPageInfo = this.getPageInfo(tblogList);
        return this.success(tblogPageInfo);
    }

    /*分页查看某个任务日志*/
    @RequestMapping("pagetbworklog")
    public String pageTbWorkLog(@RequestBody JSONObject paramJb){
        int workId = paramJb.getIntValue("workId");
        this.pageSet(paramJb);
        List<Tblog> tblogList = tbWorkService.listTbWorklog(workId);
        PageInfo<Tblog> tblogPageInfo = this.getPageInfo(tblogList);
        return this.success(tblogPageInfo);
    }

    @RequestMapping("updateworklogstatusid")
    public String updateWorkLogStatusid(@RequestBody JSONObject paramJb){
        int workId = paramJb.getIntValue("workId");
        int statusId = paramJb.getIntValue("statusId");
        Tbwork tbwork = tbWorkService.getTbwork(workId);
        tbwork.setStatusid(statusId);
        tbWorkService.updateTbwork(tbwork);
        return this.success();
    }
}
